/**
 * ChatListviewAdapter is responsible for updating the content and presentation of
 * the chatting history Listview in ChatActivity
 * <p>
 * the major function here is getView to control the display of each child view in listview
 */

package com.tjian.fafa.user.base.chatter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.bean.ChatEntity;
import com.tjian.fafa.user.config.ConfigOfFafa;
import com.tjian.fafa.user.util.image.ImageUtil;

import java.util.List;

public class ChatListviewAdapter extends BaseAdapter
{
    private List<ChatEntity> chatEntityList;
    private List<Boolean> isSelfBooleanList;
    private LayoutInflater layoutInflater;
    private Context context_class;
    public String imgpath;

    public ChatListviewAdapter(Context context, List<ChatEntity> vector,
                               List<Boolean> vectorIsSelf)
    {
        this.chatEntityList = vector;
        layoutInflater = LayoutInflater.from(context);
        this.context_class = context;
        this.isSelfBooleanList = vectorIsSelf;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup root)
    {
        ImageView avatar;
        TextView content;
        TextView NameOfSpeaker;
        ChatEntity chatEntity = chatEntityList.get(position);
        String name = chatEntity.getName();
        String time = chatEntity.getTime();
        int sex = chatEntity.getSex();
        String real_msg = chatEntity.getContent();

        float roundPx = 200; //图片变圆角参数；

        if (isSelfBooleanList.get(position).booleanValue())//是自己对话框在右边
        {
            convertView = layoutInflater.inflate(R.layout.cb0_chat_listview_item_right, null);
            content = (TextView) convertView.findViewById(R.id.cb0ChatListviewMsgRight);
            NameOfSpeaker = (TextView) convertView.findViewById(R.id.cb0ChatListviewNameRight);
            avatar = (ImageView) convertView.findViewById(R.id.cb0ChatListviewAvatarRight);

            String zhengze = "f0[0-9]{2}|f10[0-7]";                                            //正则表达式，用来判断消息内是否有表情
            try
            {
                SpannableString spannableString = ChatEmoticonUtil.getExpressionString(context_class, real_msg, zhengze);
                content.setText(spannableString);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
            catch (SecurityException e)
            {
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            }

            NameOfSpeaker.setText(time);

            int avatarId = chatEntity.getSenderAvatarid();
            if (avatarId == 0)
            {
                String urlString = ConfigOfFafa.projectWebsiteUrl + chatEntity.getmAvatarImagePath();
                Bitmap bitmap = ImageUtil.getBitMapForStringURL(urlString);

                bitmap = ImageUtil.convertToRoundedCorner(bitmap, roundPx);
                avatar.setImageBitmap(bitmap);
                //				avatar.setImageResource(R.drawable.cb0_h001);
            }
            else
            {
                String urlString = ConfigOfFafa.projectWebsiteUrl + chatEntity.getmAvatarImagePath();
                Bitmap bitmap = ImageUtil.getBitMapForStringURL(urlString);

                bitmap = ImageUtil.convertToRoundedCorner(bitmap, roundPx);
                avatar.setImageBitmap(bitmap);//	avatar.setImageResource(R.drawable.cb0_h003);

            }

        }
        else//聊天是对方，则对话框在左边
        {
            convertView = layoutInflater.inflate(R.layout.cb0_chat_listview_item_left, null);

            content = (TextView) convertView.findViewById(R.id.cb0ChatListviewMsgLeft);

            String zhengze = "f0[0-9]{2}|f10[0-7]";                                            //正则表达式，用来判断消息内是否有表情
            try
            {
                SpannableString spannableString = ChatEmoticonUtil.getExpressionString(context_class, real_msg, zhengze);
                content.setText(spannableString);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
            }
            catch (SecurityException e)
            {
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                e.printStackTrace();
            }

            NameOfSpeaker = (TextView) convertView.findViewById(R.id.cb0ChatListviewNameLeft);
            NameOfSpeaker.setText(name + " " + time);

            avatar = (ImageView) convertView.findViewById(R.id.cb0ChatListviewAvatarLeft);
            int avatarId = chatEntity.getSenderAvatarid();
            if (avatarId == 0)
            {
                String urlString = ConfigOfFafa.projectWebsiteUrl + chatEntity.getmAvatarImagePath();
                Bitmap bitmap = ImageUtil.getBitMapForStringURL(urlString);

                bitmap = ImageUtil.convertToRoundedCorner(bitmap, roundPx);
                avatar.setImageBitmap(bitmap);
                imgpath = chatEntity.getmAvatarImagePath();
                //				avatar.setImageResource(R.drawable.cb0_h001);
            }
            else
            {
                String urlString = ConfigOfFafa.projectWebsiteUrl + chatEntity.getmAvatarImagePath();
                Bitmap bitmap = ImageUtil.getBitMapForStringURL(urlString);

                bitmap = ImageUtil.convertToRoundedCorner(bitmap, roundPx);
                avatar.setImageBitmap(bitmap);//	avatar.setImageResource(R.drawable.cb0_h003);
                imgpath = chatEntity.getmAvatarImagePath();
            }
        }

        return convertView;
    }

    public int getCount()
    {
        return chatEntityList.size();
    }

    public Object getItem(int position)
    {
        return chatEntityList.get(position);
    }

    public long getItemId(int position)
    {
        return position;
    }
}